package com.example.views.search;

import java.util.ArrayList;

import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.adapter.SearchAdapter;
import com.example.dto.ItemSearch;
import com.example.ishoopingcaffe.R;
import com.example.listener.ActionEventConstantSearch;
import com.example.listener.OnEventControlListener;

public class ViewSearchMain extends LinearLayout implements OnClickListener{
	ListView listMenu;
	SearchAdapter adapter;
	Context mContext;
	LinearLayout layoutContain;
	OnEventControlListener mListener;
	ProgressBar progress_bar_search;
	TextView textHeader, txt_search_for;
	Button btnScan;
	public ViewSearchMain(Context context, OnEventControlListener listenner) {
		super(context);
		mContext = context;
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_main_search, this,true);
		mListener = listenner;
		layoutContain = (LinearLayout)findViewById(R.id.layoutContain);
		progress_bar_search =  (ProgressBar)findViewById(R.id.progress_bar_search);
		progress_bar_search.setVisibility(View.GONE);
		txt_search_for = (TextView)findViewById(R.id.txt_search_for);
		txt_search_for.setVisibility(View.GONE);
		textHeader = (TextView)findViewById(R.id.txtTitle);
		textHeader.setText(mContext.getString(R.string.search));
		
		btnScan = (Button) findViewById(R.id.btnOptionLeft);
		btnScan.setOnClickListener(this);
		listMenu = (ListView) findViewById(R.id.listview_Search);
		ArrayList<ItemSearch> arrItem = new ArrayList<ItemSearch>();
		for (int i = 0; i < 20; i++) {
			ItemSearch content = new ItemSearch();
			content.setId("" + i);
			content.setTitle("Thức uống " + i);
			arrItem.add(content);
		}
		adapter = new SearchAdapter(mContext,R.layout.item_search,arrItem);
		listMenu.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		listMenu.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mListener.onEvent(ActionEventConstantSearch.ACTION_CHANGE_VIEW_FILTER, null, null);
			}
		});
		
		layoutContain.setFocusableInTouchMode(true);
		layoutContain.requestFocus();
		layoutContain.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(keyCode == KeyEvent.KEYCODE_BACK 
						&& event.getAction() == KeyEvent.ACTION_UP)
				{
//					mListener.onBackListener();
					return true;
				}
				return false;
			}
		});
		
	}
	public void destroy()
	{
		removeAllViews();
		destroyDrawingCache();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnOptionLeft:
			mListener.onEvent(ActionEventConstantSearch.ACTION_CHANGE_VIEW_FILTER, null, null);
			break;

		default:
			break;
		}
		
	}

}
